package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.StarTravelDtoMapper;
import com.starlink.model.domain.StarTravelDO;
import com.starlink.mapper.StarTravelMapper;
import com.starlink.model.request.StarTravelRequest;
import com.starlink.service.StarTravelService;
import com.starlink.model.util.StarTravelConvert;
import com.starlink.model.vo.StarTravelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class StarTravelServiceImpl extends ServiceImpl<StarTravelMapper, StarTravelDO> implements StarTravelService {
    @Autowired
    private StarTravelMapper starTravelMapper;

    @Override
    public StarTravelVO getByKey(long id) {
        StarTravelVO starTravelVO = StarTravelDtoMapper.INSTANCE.do2VO(getById(id));
        return starTravelVO;
    }

    @Override
    public PageInfo<StarTravelVO> pageQuery(Pageable pageable,StarTravelRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<StarTravelDO> list = starTravelMapper.listQuery(request);
        List<StarTravelVO> starTravelVOList = StarTravelConvert.buildStarTravelVoList(list);
        return new PageInfo(starTravelVOList);
    }

    @Override
    public List<StarTravelVO> listQuery(StarTravelRequest request) {
        List<StarTravelDO> list = starTravelMapper.selectDOList(StarTravelDtoMapper.INSTANCE.request2DO(request));
        List<StarTravelVO> starTravelVOS = StarTravelConvert.buildStarTravelVoList(list);
        return starTravelVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(StarTravelRequest request) {
        save(StarTravelDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(StarTravelRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(StarTravelDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        starTravelMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
